package com.cdream.basic.mq.kafka;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Properties;

import static com.cdream.basic.mq.kafka.KafkaConst.BROKER_LIST;
import static com.cdream.basic.mq.kafka.KafkaConst.TOPIC;

/**
 * @author cdream
 * @date 2022/8/7
 */
@Slf4j
public class KafkaDogConsumerDemo {
    private static final String GROUP_ID = "group.demo7";

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("key.deserializer", StringDeserializer.class.getName());
        props.put("value.deserializer", DogDeserializer.class.getName());
        props.put("bootstrap.servers", BROKER_LIST);
        props.put("group.id", GROUP_ID);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,false);
        // props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        KafkaConsumer<String, Dog> consumer= new KafkaConsumer<>(props);
        consumer.subscribe(CollUtil.newArrayList(TOPIC));
        while (true){
            ConsumerRecords<String, Dog> records = consumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord<String, Dog> record : records) {
                System.out.println(record.value());
                System.out.println(record.key());
            }
            consumer.commitSync();

        }
    }
}
